﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectShare
{
    public class Common
    {
        /// <summary>
        /// decimal?  转double
        /// </summary>
        /// <param name="Num"></param>
        /// <returns></returns>
        public static Double GetDouble(decimal? Num)
        {
            Double result = 0;
            if (Num != null)
            {
                result = Convert.ToDouble(Num);
            }
            return result;
        }
        /// <summary>
        /// 四舍五入返回一位小数
        /// </summary>
        public static string GetOne(decimal? value)
        {
            string result = "0.0";
            if (value != null)
            {
                result = Math.Round(GetDouble(value), 1).ToString();
            }
            return result;
        }
        /// <summary>
        /// 四舍五入返回整数
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetInt(decimal? value)
        {
            string result = "0";
            if (value != null)
            {
                result = Math.Round(GetDouble(value), 0).ToString();
            }
            return result;
        }
        /// <summary>
        /// decimal?转decimal
        /// </summary>
        /// <param name="Num"></param>
        /// <returns></returns>
        public static decimal GetDecimal(decimal? Num)
        {
            if (Num != null)
            {
                return Convert.ToDecimal(Num);
            }
            else
            {
                return decimal.Zero;
            }
        }
        public static  ICellStyle GetCommonCellStyle(HSSFWorkbook templateBook, ICellStyle style)
        {
            IDataFormat format = templateBook.CreateDataFormat();//创建格式化 实例对象
            var CellStyle = templateBook.CreateCellStyle();
            CellStyle.DataFormat = format.GetFormat("#,##0.0");
            CellStyle.BorderBottom = style.BorderBottom;
            CellStyle.BorderRight = style.BorderRight;
            CellStyle.BorderRight = style.BorderLeft;
            CellStyle.BorderRight = style.BorderTop;
            CellStyle.VerticalAlignment = VerticalAlignment.Center;
            CellStyle.Alignment = HorizontalAlignment.Center;
            CellStyle.FillPattern = FillPattern.SolidForeground;
            return CellStyle;
        }
        /// <summary>
        /// 州市列保留一位小数excell样式
        /// </summary>
        /// <param name="templateBook"></param>
        /// <param name="style">原来表中的样式</param>
        /// <returns></returns>
        public static ICellStyle GetOneCityCellStyle(HSSFWorkbook templateBook, ICellStyle style)
        {
            var CellStyle = GetCommonCellStyle(templateBook, style);
            CellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightTurquoise.Index;
            return CellStyle;
        }
        /// <summary>
        /// 区县列保留一位小数excell样式
        /// </summary>
        /// <param name="templateBook"></param>
        /// <param name="style">原来表中的样式</param>
        /// <returns></returns>
        public static ICellStyle GetOneAreaCellStyle(HSSFWorkbook templateBook, ICellStyle style)
        {
            var CellStyle = GetCommonCellStyle(templateBook, style);
            CellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.White.Index;
            return CellStyle;
        }
        /// <summary>
        /// 州市列整数excell样式
        /// </summary>
        /// <param name="templateBook"></param>
        /// <param name="style">原来表中的样式</param>
        /// <returns></returns>
        public static ICellStyle GetIntCityCellStyle(HSSFWorkbook templateBook, ICellStyle style)
        {
            IDataFormat format = templateBook.CreateDataFormat();//创建格式化 实例对象
            var CellStyle = GetCommonCellStyle(templateBook, style);
            CellStyle.DataFormat = format.GetFormat("#,##0");
            CellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightTurquoise.Index;
            return CellStyle;
        }
        /// <summary>
        /// 区县列整数excell样式
        /// </summary>
        /// <param name="templateBook"></param>
        /// <param name="style">原来表中的样式</param>
        /// <returns></returns>
        public static ICellStyle GetIntAreaCellStyle(HSSFWorkbook templateBook, ICellStyle style)
        {
            IDataFormat format = templateBook.CreateDataFormat();//创建格式化 实例对象
            var CellStyle = GetCommonCellStyle(templateBook, style);
            CellStyle.DataFormat = format.GetFormat("#,##0");
            CellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.White.Index;
            return CellStyle;
        }
    }
}
